<?php

include_once 'ZReader.php';
include_once 'ZTrendCollection.php';
include_once 'ZTrendItem.php';

class ZTrendReader extends ZReader {
    protected $cache    = 'trend';

    /**
     * Generate command string
     * @return string
     */
    public function command() {
        return sprintf('trend %s %s %d %d %s'."\r\n",
            $this->begin,
            $this->end,
            $this->timeZone,
            $this->language,
            $this->query
        );
    }
    /**
     * Parse data that receive from server to object
     * @param $callback
     * @param $state
     * @return int
     */
    public function parse($callback, &$state)
    {
        $i = 0;
        $collections = new ZTrendCollection();

        $succeed = false;

        //Read data while not end of file
        while (!$this->eof()) {
            //Read the first block is the length of the data
            $numBytes = $this->readNum();

            if ($numBytes >= 0 && $numBytes < 0xffffffff) {
                //Read data
                $arr[] = $this->readNum($numBytes);
                $i++;

                //Enough field
                if ($i == 2) {
                    $trendItem = new ZTrendItem($arr[0], $arr[1]);

                    $curDate = strtotime($arr[0]);
                    $size = $collections->count();

                    $timeCompare = 0;

                    //Set trend 0 from begin to point which has trend > 0
                    if ($size == 0) {
                        $itemCompare = mb_substr($this->begin, 0, 8);//Get Ymd of begin
                    } else {
                        //compare Item
                        $itemCompare = $collections->getIndex($size - 1)->getDate();
                    }
                    $timeCompare = strtotime($itemCompare);
                    //check offset date
                    while ($curDate - $timeCompare > 86400) {
                        $timeCompare += 86400;
                        //create date with trend = 0
                        $collections->addItem(new ZTrendItem(date('Ymd', $timeCompare), 0));
                    }

                    $collections->addItem($trendItem);
                    $arr = array();
                    $i = 0;
                }

                //Not succeeded
                $succeed = false;
            } else if ($numBytes == 0xffffffff) {
                $succeed = true;
            }
        }

        if ($succeed) {
            //Set trend 0 from end of array to end time
            $curDate        = strtotime(mb_substr($this->end, 0, 8));
            $size           = $collections->count();
            $timeCompare    = strtotime($collections->getIndex($size-1)->getDate());
            //check offset date
            while ($curDate - $timeCompare > 86400) {
                $timeCompare += 86400;
                //create date with trend = 0
                $collections->addItem(new ZTrendItem(date('Ymd', $timeCompare), 0));
            }
        }

        $state = $succeed ? ($collections->count()> 0 ? 1 : 0) : -1;

        return $collections;
    }

    /**
     * @param null $callback
     * @return ZTrendCollection
     * @throws Exception
     */
    public function read($callback = null) {
        return parent::read($callback);
    }
}

?>
